<?php


namespace app\common\model\xiluedu;


use app\common\library\Auth;
use think\Model;
use function fast\array_get;

class UserCourse extends Model
{
    // 表名
    protected $name = 'xiluedu_user_course';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];

    public function course2(){
        return $this->belongsTo('Course','course_id','id',[],'left')->setEagerlyType(0);
    }

    public function course(){
        return $this->belongsTo('Course','course_id','id',[],'left')->setEagerlyType(1);
    }

    /**
     * 我的课程列表
     */
    public static function lists($params){
        $auth = Auth::instance();
        $pagesize = array_get($params,'pagesize',10);
        return static::with(['course'=>function($query){
            $query->withField(['id','type','name','category_id','tag_ids','teacher_id','thumb_image','description','is_charge','(basic_num+real_num) total_num','collection_count','salesprice','play_count']);
        }])
            ->where('user_id',$auth->id)
            ->order('id','desc')
            ->paginate($pagesize);
    }


    public static function isBuy($user_id,$course_id){
       return static::where('user_id',$user_id)->where('course_id',$course_id)->find() ? 1 : 0;
    }

}